package laxexec

import (
	"os"
	"os/exec"
	"strconv"
)

// @Desc: 创建一个laxcommand
// @param cmd 指令整串
// @return *LaxCommand
func NewLaxCommand(cmd string) *LaxCommand {
	return &LaxCommand{
		Cmd: exec.Command("cmd", "/c", cmd),
	}
}

// @Desc: 附带参数的指令
// @param name 指令名，比如cmd, dir, xx.exe
// @param args 启动参数
// @return *LaxCommand
func NewLaxCommandWithArgs(name string, args ...string) *LaxCommand {
    return &LaxCommand{
        Cmd: exec.Command(name, args...),
    }
}

// @Desc: 杀进程
// @param sig 杀进程信号,在windows上没有用
// @return error 杀进程结果
func (lc *LaxCommand) Terminate(sig os.Signal) error {
	if lc.Process == nil {
		return nil
	}
	killExec := exec.Command("taskkill", "/t", "/f", "/pid", strconv.Itoa(lc.Process.Pid))
	killExec.Stdout = os.Stdout
	killExec.Stderr = os.Stderr
	return killExec.Run()
}
